//
//  LCZNewsViewController.m
//  PersonalNews
//
//  Created by Golibyo on 15/11/26.
//  Copyright © 2015年 changshuhua. All rights reserved.
//

#import "LCZNewsViewController.h"
#import "LCZBaseTableView.h"
#import "LCZNewsModel.h"
#import "LCZNewCollectionCell.h"
#import "LCZPicModel.h"
#import "LCZNormalDetailViewController.h"
#import "LCZPicDetailViewController.h"
#import "LCZSpecialDetailViewController.h"
#import "LCZMenuCollectionViewCell.h"

@interface LCZNewsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, LCZNewCollectionCellDeletage, LCZNewCollectionCellPicDeletage, LCZNewCollectionCellSpecialDelegate>

@property(nonatomic, strong) LCZBaseCollectionView *collectionView;
@property(nonatomic, strong) NSMutableArray *newsArr;
@property(nonatomic, strong) NSMutableArray *photosetArr;
@property(nonatomic, strong) NSMutableArray *specialArr;
@property(nonatomic, strong) NSMutableArray *picArr;
@property(nonatomic, strong) NSArray *netArr;
@property(nonatomic, strong) NSArray *firstKeyArr;
@property(nonatomic, strong) LCZBaseCollectionView *lineCollection;
@property(nonatomic, strong) NSArray *titleArr;

@end

@implementation LCZNewsViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.firstKeyArr = @[@"T1348647853363", @"T1348648756099", @"T1348649580692", @"T1348650593803", @"T1348649079062"];
        self.netArr = @[@"http://c.m.163.com/nc/article/headline/T1348647853363/0-140.html", @"http://c.m.163.com/nc/article/list/T1348648756099/0-20.html", @"http://c.m.163.com/nc/article/list/T1348649580692/0-20.html", @"http://c.m.163.com/nc/article/list/T1348650593803/0-20.html", @"http://c.3g.163.com/nc/article/list/T1348649079062/0-20.html"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    创建日历本地存储文件夹
    [LCZCacheSaveTool isHaveFieldCalendar:@"Calendar"];

    self.title = @"首页";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"首页"

                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(presentLeftMenuViewController:)];
    self.view.backgroundColor = [UIColor yellowColor];
    
    self.navigationController.navigationBar.translucent = NO;

    [self createCollectionView];
    
    
    [self createDate];
    
    self.titleArr = @[@"头条", @"财经", @"科技", @"时尚", @"体育"];
    
    
    
    
    
}
#pragma mark -- create方法
- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREENWIDTH, SCREENHEIGHT - 55);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
// - 55
    self.collectionView = [[LCZBaseCollectionView alloc] initWithFrame:CGRectMake(0, 55, SCREENWIDTH, SCREENHEIGHT - 55) collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    self.collectionView.pagingEnabled = YES;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerClass:[LCZNewCollectionCell class] forCellWithReuseIdentifier:@"collectionView"];
    
    
#warning 111111111
    // lineCollectionView
    
    UICollectionViewFlowLayout *lineLayout = [[UICollectionViewFlowLayout alloc] init];
    lineLayout.itemSize = CGSizeMake(70, 30);
    lineLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
    lineLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    lineLayout.minimumLineSpacing = 30;
    
    self.lineCollection = [[LCZBaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 55) collectionViewLayout:lineLayout];
    [self.view addSubview:self.lineCollection];
    self.lineCollection.backgroundColor = [UIColor whiteColor];
    self.lineCollection.showsHorizontalScrollIndicator = NO;
  
    
    self.lineCollection.delegate = self;
    self.lineCollection.dataSource = self;
    
    [self.lineCollection registerClass:[LCZMenuCollectionViewCell class] forCellWithReuseIdentifier:@"menucollectionView"];
    
   
    
}




- (void)createDate {
    
    NSInteger i = self.collectionView.contentOffset.x / SCREENWIDTH;
    NSLog(@"%ld", i);
    

//    NSString *URLStr = @"http://c.m.163.com/nc/article/headline/T1348647853363/0-140.html";
//    NSString *URLStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/headline//%@", self.netArr[i]];
    NSLog(@"%@", self.netArr[i]);
    [ZLPNetWorkTool getWithURL:self.netArr[i] success:^(id responseObject) {
        
        NSDictionary *dic = responseObject;
        self.newsArr = [NSMutableArray array];
        self.newsArr = [LCZNewsModel baseModelArr:dic[self.firstKeyArr[i]]];
        if (self.newsArr.count) {
            [self.newsArr removeObjectAtIndex:0];
        }
        
        self.picArr = [NSMutableArray array];
        self.picArr = [LCZPicModel baseModelArr:dic[self.firstKeyArr[i]][0][@"ads"]];
        
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"%@", error);
    }];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self.newsArr removeAllObjects];
    if (scrollView == self.collectionView) {
        [self createDate];
    }
    
    self.lineCollection.contentOffset = CGPointMake(self.collectionView.contentOffset.x / SCREENWIDTH * 100, 0);
    
}



#pragma mark -- collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.collectionView) {
        LCZNewCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView" forIndexPath:indexPath];
        
        
        cell.picArr = self.picArr;
        cell.newsArr = self.newsArr;
        //    [cell.tableView reloadData];
        
        cell.delegate = self;
        cell.picdelegate = self;
        cell.specialdelegate = self;
        
        return cell;
    } else {
        LCZMenuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"menucollectionView" forIndexPath:indexPath];

        cell.menuLabel.text = self.titleArr[indexPath.row];
        cell.nightBackgroundColor = [UIColor blackColor];
        cell.menuLabel.nightTextColor = [UIColor grayColor];
        
        
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if (collectionView == self.lineCollection) {
        
        [self.newsArr removeAllObjects];
        self.collectionView.contentOffset = CGPointMake(indexPath.row * SCREENWIDTH, 0);
        [self createDate];
        
//        LCZMenuCollectionViewCell *cell = (LCZMenuCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//        cell.menuLabel.textColor = [UIColor redColor];
        
    } else {
        NSLog(@"~~~~~~~~~~~~~");
    }
    
}


#pragma mark -- 协议方法
- (void)pushToDetail:(NSString *)url {
    LCZNormalDetailViewController *controller = [[LCZNormalDetailViewController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
    controller.url = url;
}

- (void)pushToPicDetail:(NSString *)skipID andTitle:(NSString *)title {
    LCZPicDetailViewController *controller = [[LCZPicDetailViewController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
//    controller.skipID = skipID;
//    controller.titleStr = title;
    NSString *firstStr = [skipID substringFromIndex:9];
    NSString *secondStr = [skipID substringWithRange:NSMakeRange(4, 4)];
    NSString *strURL = [NSString stringWithFormat:@"http://c.3g.163.com/photo/api/set/%@/%@.json", secondStr, firstStr];
    controller.titleStr = title;
    controller.URLStr = strURL;
    
}

- (void)pushToSpecialDetail:(NSString *)specialID andTitle:(NSString *)title {
    LCZSpecialDetailViewController *controller = [[LCZSpecialDetailViewController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
    NSString *strURL = [NSString stringWithFormat:@"http://c.3g.163.com/nc/special/%@.html", specialID];
    controller.URLStr = strURL;
    controller.specialID = specialID;
    controller.titleStr = title;


}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
